package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.PageRequest;
import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.CmsSubject;
import cn.zcscloud.mall.service.CmsSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/subject")
public class CmsSubjectController {

    @Autowired
    private CmsSubjectService cmsSubjectService;

    /**
     * 根据专题名称分页获取商品专题
     *
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public R<PageRequest<CmsSubject>> list(String keyword, Integer pageNum, Integer pageSize) {
        //分页构造器
        IPage<CmsSubject> iPage = new Page<CmsSubject>(pageNum, pageSize);
        //查询构造器
        LambdaQueryWrapper<CmsSubject> lqw = new LambdaQueryWrapper<>();
        lqw.like(keyword != null,CmsSubject::getTitle, keyword);
        //查询并构造返回值
        cmsSubjectService.page(iPage, lqw);
        PageRequest pr = PageRequest.init(iPage);
        return R.success(pr);
    }

    @GetMapping("/listAll")
    public R all(){
        List<CmsSubject> list = cmsSubjectService.list();
        return R.success(list);
    }
}
